﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using OLTS.DATA_SERVICE;
using OLTS.MODEL;
using OLTS.MODEL.Custom_Model;
using OLTS.WEB.BaseClass;

namespace ONTS.WEB.Service
{
    /// <summary>
    /// Summary description for AsSignProjectNameKeyInService
    /// </summary>
    public class AsSignProjectNameKeyInService : IHttpHandler
    {


        public void ProcessRequest(HttpContext context)
        {
            try
            {
                HttpRequest request = context.Request;
                HttpResponse response = context.Response;
                var _model = new OTS_PROJECT();
                System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
                if (request["oper"] == "add")
                {
                    _model.NAME = request["PROJECT_NAME"];
                    _model.SITE_ID = Convert.ToInt16(request["SITE_NAME"]);
                    _model.START_DATE = Convert.ToDateTime(request["START_DATE"]);
                    _model.END_DATE = Convert.ToDateTime(request["END_DATE"]);
                    _model.STATUS = Convert.ToBoolean(request["STATUS"]);
                    _model.CREATE_BY = BaseService.LOGIN.EMPLOYEE_ID;
                    var cmd = new Project_Service(_model);
                    cmd.Add();
                }
                else
                {
                    if (request["oper"] == "del")
                    {
                        _model.ID = Convert.ToInt16(request["ID"]);
                        var cmd = new Project_Service(_model);
                        cmd.Delete();
                    }
                    else if (request["oper"] == "edit")
                    {
                        _model.ID = Convert.ToInt16(request["ID"]);
                        _model.NAME = request["PROJECT_NAME"];
                        _model.SITE_ID = Convert.ToInt16(request["SITE_NAME"]);
                        _model.START_DATE = Convert.ToDateTime(request["START_DATE"]);
                        _model.END_DATE = Convert.ToDateTime(request["END_DATE"]);
                        _model.STATUS = Convert.ToBoolean(request["STATUS"]);

                        var cmd = new Project_Service(_model);
                        cmd.Edit();
                    }
                }

                var _cmd = new Project_Service();
                var lst = new List<ASSIGN_PROJECT>();
                var row = 0;
                foreach (var d in _cmd.GetAllinclude().OrderByDescending(x => x.START_DATE).ToList())
                {
                    row = row + 1;
                    var model = new ASSIGN_PROJECT();
                    model.ID = d.ID;
                    model.RowIndex = row;
                    model.SITE_NAME = d.OTS_SITE.NAME;
                    model.PROJECT_NAME = d.NAME;
                    model.START_DATE = d.START_DATE;
                    model.END_DATE = d.END_DATE;
                    model.STATUS = d.STATUS;
                    lst.Add(model);
                }
                lst = lst.OrderBy(o => o.RowIndex).ToList();


                int total = lst.Count == 0 ? 1 : (int)Math.Ceiling((double)lst.Count / 10);

                var jsonData = new
                {
                    total = total,
                    page = request["page"],
                    records = lst.Count,
                    rows = lst.Skip(Convert.ToInt16(request["page"]) * 10 - 10).Take(10)
                };

                response.Write(new JavaScriptSerializer().Serialize(jsonData));
            }
            catch (Exception e)
            {
                
                throw e;
            } 
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}